package com.my.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class CalendarUtil {
	/** 
	 * 常用日历操作辅助类 
	 *  
	 * @author yuxh
	 */   
	
	
	/**获取当前时间，精确到时分秒**/
	public static String calendarGetCurrentTimeString() {
		Calendar ca = Calendar.getInstance();
		int year = ca.get(Calendar.YEAR);// 获取年份
		int month = ca.get(Calendar.MONTH);// 获取月份
		int day = ca.get(Calendar.DATE);// 获取日
		int minute = ca.get(Calendar.MINUTE);// 分
		int hour = ca.get(Calendar.HOUR_OF_DAY);// 小时
		int second = ca.get(Calendar.SECOND);// 秒
		String fMonth = String.valueOf(month + 1);
		if (month < 9) {
			fMonth = "0" + (month+1);
		}
		String fDay = String.valueOf(day);
		if (day < 10) {
			fDay = "0" + day;
		}
		String fMinute = String.valueOf(minute);
		if (minute < 10) {
			fMinute = "0" + minute;
		}
		String fHour = String.valueOf(hour);
		if (hour < 10) {
			fHour = "0" + hour;
		}
		String fSecond = String.valueOf(second);
		if (second < 10) {
			fSecond = "0" + second;
		}
		String currentTime = year + "-" + fMonth + "-" + fDay + " " + fHour + ":" + fMinute + ":" + fSecond;
		return currentTime;
	}
	
	/**获取当前时间，精确到时分秒**/
	public static String calendarCaculation(Calendar ca) {
		int year = ca.get(Calendar.YEAR);// 获取年份
		int month = ca.get(Calendar.MONTH);// 获取月份
		int day = ca.get(Calendar.DATE);// 获取日
		int minute = ca.get(Calendar.MINUTE);// 分
		int hour = ca.get(Calendar.HOUR_OF_DAY);// 小时
		int second = ca.get(Calendar.SECOND);// 秒
		String fMonth = String.valueOf(month + 1);
		if (month < 9) {
			fMonth = "0" + (month+1);
		}
		String fDay = String.valueOf(day);
		if (day < 10) {
			fDay = "0" + day;
		}
		String fMinute = String.valueOf(minute);
		if (minute < 10) {
			fMinute = "0" + minute;
		}
		String fHour = String.valueOf(hour);
		if (hour < 10) {
			fHour = "0" + hour;
		}
		String fSecond = String.valueOf(second);
		if (second < 10) {
			fSecond = "0" + second;
		}
		String currentTime = year + "-" + fMonth + "-" + fDay + " " + fHour + ":" + fMinute + ":" + fSecond;
		return currentTime;
	}
	
	/**获取当前时间,精确到分,格式为设备协议方式**/
	public static String calendarGetGprsTime() {
		Calendar ca = Calendar.getInstance();
		int year = ca.get(Calendar.YEAR);// 获取年份
		int month = ca.get(Calendar.MONTH);// 获取月份
		int day = ca.get(Calendar.DATE);// 获取日
		int minute = ca.get(Calendar.MINUTE);// 分
		int hour = ca.get(Calendar.HOUR_OF_DAY);// 小时
		String fMonth = String.valueOf(month + 1);
		System.out.println(fMonth);
		if (month < 9) {
			fMonth = "0" + (month+1);
		}
		String fDay = String.valueOf(day);
		if (day < 10) {
			fDay = "0" + day;
		}
		String fMinute = String.valueOf(minute);
		if (minute < 10) {
			fMinute = "0" + minute;
		}
		String fHour = String.valueOf(hour);
		if (hour < 10) {
			fHour = "0" + hour;
		}
		System.out.println(fMonth);
		String currentTime = year + "" + fMonth + "" + fDay + "" + fHour + "" + fMinute;
		return currentTime;
	}
	
	/**获取当前时间，精确到天**/
	public static String calendarGetDay(Calendar ca) {
		int year = ca.get(Calendar.YEAR);// 获取年份
		int month = ca.get(Calendar.MONTH);// 获取月份
		int day = ca.get(Calendar.DATE);// 获取日
		String fMonth = String.valueOf(month + 1);
		if (month < 9) {
			fMonth = "0" + (month+1);
		}
		String fDay = String.valueOf(day);
		if (day < 10) {
			fDay = "0" + day;
		}
		String currentTime = year + "-" + fMonth + "-" + fDay;
		return currentTime;
	}
	
	/**获取当前时间，精确到天**/
	public static String getCurrentDay() {
		Calendar ca = Calendar.getInstance();
		int year = ca.get(Calendar.YEAR);// 获取年份
		int month = ca.get(Calendar.MONTH);// 获取月份
		int day = ca.get(Calendar.DATE);// 获取日
		String fMonth = String.valueOf(month + 1);
		if (month < 9) {
			fMonth = "0" + (month+1);
		}
		String fDay = String.valueOf(day);
		if (day < 10) {
			fDay = "0" + day;
		}
		String currentTime = year + "-" + fMonth + "-" + fDay;
		return currentTime;
	}
	
	
	/**获取这一周的第一天的日期**/
	public static String getFirstDayOfWeek() {
		Calendar ca = Calendar.getInstance();
		int initDay = ca.getFirstDayOfWeek();
		ca.set(Calendar.DAY_OF_WEEK, initDay+1);
		String firstTimeOfWeek=CalendarUtil.calendarGetDay(ca)+" 00:00:00";
		return firstTimeOfWeek;
	}
	
	/**获取这一周的最后一天的日期**/
	public static String getLastDayOfWeek() {
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.WEEK_OF_YEAR, 1);
		int initDay = ca.getFirstDayOfWeek();
		System.out.println(initDay);
		ca.set(Calendar.DAY_OF_WEEK, initDay);
		String LastDayOfWeek=CalendarUtil.calendarGetDay(ca)+" 23:59:59";
		return LastDayOfWeek;
	}
	
	/**获取这一月的第一天的日期**/
	public static String getFirstDayOfMonth() {
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
		String firstDayOfMonth=CalendarUtil.calendarGetDay(ca)+" 00:00:00";
		return firstDayOfMonth;
	}
	
	/**获取这一月的最后一天的日期**/
	public static String getLastDayOfMonth() {
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DATE, ca.getActualMaximum(Calendar.DATE)); 
		String LastDayOfMonth=CalendarUtil.calendarGetDay(ca)+" 23:59:59";
		return LastDayOfMonth;
	}
	
	/**获取所给时间前一天的日期**/
	@SuppressWarnings("static-access")
	public static String getDayLastDay(String givenTime){
		String time="";
		try {
			SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date=sim.parse(givenTime);
			Calendar calendar = new GregorianCalendar(); 
		    calendar.setTime(date); 
		    //calendar.add(calendar.YEAR, 1);//把日期往后增加一年.整数往后推,负数往前移动
		    //calendar.add(calendar.DAY_OF_MONTH, 1);//把日期往后增加一个月.整数往后推,负数往前移动
		    calendar.add(calendar.DATE,-1);//把日期往后增加一天.整数往后推,负数往前移动 
		    //calendar.add(calendar.WEEK_OF_MONTH, 1);//把日期往后增加一个月.整数往后推,负数往前移动
		    date=calendar.getTime();   //这个时间就是日期往后推一天的结果 
		    time=sim.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return time;
	}
	
	/**获取所给时间前一天的日期**/
	@SuppressWarnings("static-access")
	public static String getDayLastDayUtilDay(String givenTime){
		String time="";
		try {
			SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
			Date date=sim.parse(givenTime);
			Calendar calendar = new GregorianCalendar(); 
		    calendar.setTime(date); 
		    //calendar.add(calendar.YEAR, 1);//把日期往后增加一年.整数往后推,负数往前移动
		    //calendar.add(calendar.DAY_OF_MONTH, 1);//把日期往后增加一个月.整数往后推,负数往前移动
		    calendar.add(calendar.DATE,-1);//把日期往后增加一天.整数往后推,负数往前移动 
		    //calendar.add(calendar.WEEK_OF_MONTH, 1);//把日期往后增加一个月.整数往后推,负数往前移动
		    date=calendar.getTime();   //这个时间就是日期往后推一天的结果 
		    time=sim.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return time;
	}
	
	/**获取所给时间前一个月的日期,精确到时分秒**/
	@SuppressWarnings("static-access")
	public static String getDayLastMonth(String givenTime){
		String time="";
		try {
			SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date=sim.parse(givenTime);
			Calendar calendar = new GregorianCalendar(); 
		    calendar.setTime(date); 
		    //calendar.add(calendar.YEAR, 1);//把日期往后增加一年.整数往后推,负数往前移动
		    calendar.add(calendar.MONTH, -1);//把日期往前减一个月.整数往后推,负数往前移动
		    //calendar.add(calendar.DATE,-1);//把日期往后增加一天.整数往后推,负数往前移动 
		    //calendar.add(calendar.WEEK_OF_MONTH, 1);//把日期往后增加一个月.整数往后推,负数往前移动
		    date=calendar.getTime();   //这个时间就是日期往后推一天的结果 
		    time=sim.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return time;
	}
	
	/**获取所给时间前一个月的日期,精确到时分秒**/
	@SuppressWarnings("static-access")
	public static String getDayLastMonthBu(String givenTime){
		String time="";
		try {
			SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
			Date date=sim.parse(givenTime);
			Calendar calendar = new GregorianCalendar(); 
		    calendar.setTime(date); 
		    //calendar.add(calendar.YEAR, 1);//把日期往后增加一年.整数往后推,负数往前移动
		    calendar.add(calendar.MONTH, -1);//把日期往前减一个月.整数往后推,负数往前移动
		    //calendar.add(calendar.DATE,-1);//把日期往后增加一天.整数往后推,负数往前移动 
		    //calendar.add(calendar.WEEK_OF_MONTH, 1);//把日期往后增加一个月.整数往后推,负数往前移动
		    date=calendar.getTime();   //这个时间就是日期往后推一天的结果 
		    time=sim.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return time;
	}
	
	/**当前时间是否在所给起止时间范围内**/
	public static boolean currentTimeInRange(String startTime,String endTime){
		boolean inRange=false;
		
		Calendar ca = Calendar.getInstance();
		int minute = ca.get(Calendar.MINUTE);// 分
		int hour = ca.get(Calendar.HOUR_OF_DAY);// 小时
		String fMinute = String.valueOf(minute);
		if (minute < 10) {
			fMinute = "0" + minute;
		}
		String fHour = String.valueOf(hour);
		if (hour < 10) {
			fHour = "0" + hour;
		}
		int current=Integer.valueOf(fHour + fMinute);
		String startHour=startTime.substring(0,startTime.indexOf(":"));
		String startMinute=startTime.substring(startTime.indexOf(":")+1,startTime.length());
		int start=Integer.valueOf(startHour+startMinute);
		String endHour=endTime.substring(0,endTime.indexOf(":"));
		String endMinute=endTime.substring(endTime.indexOf(":")+1,endTime.length());
		int end=Integer.valueOf(endHour+endMinute);
		if(current>=start&&current<=end){
			inRange=true;
		}
		return inRange;
	}
	
	@SuppressWarnings("static-access")
	public static String minusDay(int days){
		String time=CalendarUtil.getCurrentDay();
		try {
			SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
			Date date=sim.parse(time);
			Calendar calendar = new GregorianCalendar(); 
		    calendar.setTime(date); 
		    calendar.add(calendar.DATE,(0-days));//把日期往后增加一天.整数往后推,负数往前移动 
		    date=calendar.getTime();   //这个时间就是日期往后推一天的结果 
		    time=sim.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return time;
	}

	public static Date stringToDate(String source, String pattern) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = simpleDateFormat.parse(source);
        } catch (Exception e) {
        }
        return date;
    }

	public static String DateToString(Date source, String pattern) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(source);
    }

}
